package com.hsy.cookie_session.controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hsy.cookie_session.enity.User;
import com.hsy.cookie_session.service.UserService;
import com.sun.deploy.net.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping("/")
    public String index(){
        return "login";
    }

    @RequestMapping("getAllUser")
    @ResponseBody
    public String getAllUser(){
        HashMap<String, Object> hashMap = new HashMap<>();
        List<User> userList = userService.getUser();
        hashMap.put("userList",userList);
        String result = new JSONObject(hashMap).toString();
        return result;
    }

    @RequestMapping("login")
    @ResponseBody
    public String login(User user){
        User user1 = userService.getUserById(user.getUsername());
        HashMap<String, Object> hashMap = new HashMap<>();
        if(user1 == null){
            hashMap.put("code",0);
            hashMap.put("msg","该用户不存在!!!");
        }else{
            if(!user.getPassword().equals(user1.getPassword())){
                hashMap.put("code",1);
                hashMap.put("msg","密码错误!!!");
            }else{
                hashMap.put("code",2);
                hashMap.put("msg","登陆成功");
            }
        }
        String result = new JSONObject(hashMap).toString();
        return result;
    }

    @RequestMapping("loginSuccess")
    public String loginSuccess(){
        return "loginSuccess";
    }

    @RequestMapping("test")
    @ResponseBody
    public String test(){
        System.out.println("1212122");
        return "aaa";
    }
}
